/**
 * The ProgressBar user-interface component and tag conversor class
 * @class syslib.ui.ProgressBar
 * @author Gabriel Ricci
 */
syslib.ui.ProgressBar = __class(syslib.ui.VisualTag, 
/**
 * @lends syslib.ui.ProgressBar
 */
{
	// class attributes
	id : "",
	text : "", 
	progress : 0, 

	prepare : function(){
		// set some data from the tag attributes
		this.id = (__valid(this.__attributes.id)) ? this.__attributes.id : "progressbar_"  + Math.floor(Math.random() * 101);
		this.text = (__valid(this.__attributes.text)) ? this.__attributes.text : "";
		this.progress = (__valid(this.__attributes.progress) && !isNaN(parseInt(this.__attributes.progress))) 
			? parseInt(this.__attributes.progress) 
			: 0;
		
		// load the style
		syslib.loadStyleForComponent("progressbar");
		
		// return
		return true;
	},

	execute : function(element){
		// set the attributes of the element
		var attributes	= __extend(this.__attributes, {
			"class" : "progressbar-container", 
			"id" : this.id, 
			"text" : false, 
			"progress" : false
		});
		
		// create the json structure
		elemtJSON = ["span", attributes, 
			["div", {"class" : "progressbar-text", "innerName" : "text"}, this.text], 
			["span", {"class" : "progressbar-progress", "innerName" : "progress"}, "&nbsp;"]
		];

		// create the element
		var progressBar = this.createDOM(elemtJSON);
		
		// return
		return progressBar;
	},

	finalize: function(){
		// register this parser
		syslib.ui.ProgressBar.__parsers[this.id] = this;
		
		// set the progress
		this.setProgress(this.progress);
	
		// return
		return true;
	}, 
	
	setProgress : function(progress){
		// cast to int
		var intProgress = parseInt(progress);
		
		// validations
		if(isNaN(intProgress)){
			return false;
		}
		if(intProgress > 100){
			intProgress = 100;
		}
		
		this.__refs.progress.style.width = intProgress + "%"; 
	}, 
	
	setText : function(text){
		// set the text
		this.__refs.text.innerHTML = text;  
	}
});

// object containing all the created progressbars
syslib.ui.ProgressBar.__parsers = {};

// ProgressBar static methods
syslib.ui.ProgressBar.setProgress = function(id, progress){
	syslib.ui.ProgressBar.__parsers[id].setProgress(progress);
};
syslib.ui.ProgressBar.setText = function(id, text){
	syslib.ui.ProgressBar.__parsers[id].setText(text);
};